<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<title>APP</title>
		<link rel="stylesheet" type="text/css" href="../css/aui.css" />
		<style type="text/css">
			.aui-card {
				margin-top: 15px;
			}
			.aui-input-addon.aui-iconfont {
				font-size: 20px !important;
			}
			.aui-input-row {
				width: 100% !important;
			}
		</style>
	</head>
	<body>
		<div class="aui-content aui-card">
			<div class="aui-form">
				<div class="aui-input-row">
					<i class="aui-input-addon aui-iconfont aui-icon-people aui-text-info"></i>
					<input type="text" class="aui-input" id="username" name="username" placeholder="用户名"/>
				</div>
				<div class="aui-input-row">
					<i class="aui-input-addon aui-iconfont aui-icon-lock aui-text-warning"></i>
					<input type="password" id="password" class="aui-input" id="password" name="password" placeholder="密码"/>
					<i class="aui-input-addon aui-iconfont aui-icon-attention aui-text-default" tapmode onclick="showPassword();" id="showpass-btn"></i>
				</div>
				<!--				<div class="aui-input-row">
				<i class="aui-input-addon aui-iconfont aui-icon-lock aui-text-warning"></i>
				<input type="password" class="aui-input" placeholder="确认密码"/>
				</div>-->
				<div class="aui-input-row">
					<span class="aui-input-addon">姓名:</span>
					<input type="text" class="aui-input" id="signame" name="signame" placeholder="请输入本人姓名"/>
					<!--<span class="aui-input-addon">
					<div class="aui-btn" id="sendVerify" tapmode onclick="regTemplate()">
					签名模板注册
					</div> </span>-->
				</div>
				<div class="aui-input-row">
					<p class="aui-padded-10">
						提示: 用户注册完毕后，您需要书写至少3次个人签名作为模板。
					</p>
				</div>
				<div class="aui-btn-row">
					<div class="aui-btn aui-btn-block aui-btn-primary" onclick="Register()">
						确认
					</div>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="../script/api.js" ></script>
	<script type="text/javascript" src="../script/sign.js"></script>
	<script type="text/javascript" src="../script/APICloud-rest-SHA1.js"></script>
	<script type="text/javascript">
		var APPID = "A6996268375072";
		var APPKEY = "DF2513D5-2329-E8B0-7064-A9D8D701D282";
		var isinerval, times;
		apiready = function() {
			api.parseTapmode();
		}
		function showPassword() {
			$api.attr($api.byId('password'), 'type', 'text');
			$api.removeCls($api.byId('showpass-btn'), 'aui-icon-attention');
			$api.addCls($api.byId('showpass-btn'), 'aui-icon-attentionfill');
			$api.attr($api.byId('showpass-btn'), 'onclick', 'hidePassword();');
			api.parseTapmode();
		}

		function hidePassword() {
			$api.attr($api.byId('password'), 'type', 'password');
			$api.removeCls($api.byId('showpass-btn'), 'aui-icon-attentionfill');
			$api.addCls($api.byId('showpass-btn'), 'aui-icon-attention');
			$api.attr($api.byId('showpass-btn'), 'onclick', 'showPassword();');
			api.parseTapmode();
		}

		function regTemplate() {
			alert("模板注册");
			//      var sendVerify = $api.byId('sendVerify');
			//      $api.removeAttr(sendVerify,'onclick');
			//      $api.html(sendVerify,'<span id="GetVerify">120</span>S');
			//      times = 119;
			//      isinerval = setInterval("CountDown()", 1000);
			//      setTimeout(function(){
			//          $api.val($api.byId("code"),'888888');
			//      },500)
			//
		}

		// 获取当前时间
		function getNowFormatDate() {
			var date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if (month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if (strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
			return currentdate;
		}

		function Register() {
			if (document.getElementById('username').value.length == 0) {
				document.getElementById('username').focus();
				api.toast({
					msg : '用户名不能为空'
				});
				return;
			} else if (document.getElementById('password').value.length == 0) {
				document.getElementById('password').focus();
				api.toast({
					msg : '密码不能为空！'
				});
				return;
			} else if (document.getElementById('signame').value.length == 0) {
				document.getElementById('signame').focus();
				api.toast({
					msg : '用户姓名不能为空！'
				});
				return;
			} else {
				var username = document.getElementById('username').value;
				var password = document.getElementById('password').value;
				var signame = document.getElementById('signame').value;
				// 获取当前时间    格式“yyyy-MM-dd HH:MM:SS”
				var regTime = getNowFormatDate();
				var client = new Resource(APPID, APPKEY);
				//client.setHeaders("authorization",$api.getStorage('token'));/
				var User = client.Factory("user");
				User.save({
					username : username,
					password : password,
					signame : signame
				}, function(ret, err) {
					if (ret.id) {
						var uid = ret.id;
						api.alert({
							msg : '注册成功！'
						}, function() {
							api.setPrefs({
								key : 'userinfo',
								value : {
									uid : uid,
									name : username,
									pwd : password,
									sig : signame,
									regtime : regTime
								}
							});
							// 广播事件
							api.sendEvent({
								name : 'reg_login_successEvent',
								extra : {
									uid : uid,
									name : username,
									sig : signame
								}
							});
							// 注册第二步，输入签名
							api.openWin({
								name : 'signEnroll_win',
								url : 'signEnroll_win.html'
							});
						});
					} else if (ret) {
						if (ret.error.message) {
							if (ret.error.message == "username:already exists")
								api.alert({
									msg : "用户已注册，请重新注册！"
								});
							$api.val($api.byId("name"), "");
							$api.val($api.byId("password"), "");
							//$api.val($api.byId("smsCode"), "");
						}
					} else {
						api.alert({
							msg : err.msg
						});
					}
				});
			}
		};
		function CountDown() {
			if (times < 1) {
				var sendVerify = $api.byId('sendVerify');
				$api.attr(sendVerify, 'onclick', 'get_verify()');
				$api.html(sendVerify, '重新获取');
				clearInterval(isinerval);
				return;
			}
			var getVerify = $api.byId('GetVerify');
			$api.html(getVerify, '' + times + '');
			times--;
		}
	</script>
</html>